package com.wuyan.locationdog.adapter

import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.annotation.LayoutRes
import coil.load
import com.arcsoft.commonbase.bean.AlarmChannel
import com.arcsoft.commonbase.bean.DeviceAlarmSetBeanContent
import com.arcsoft.commonbase.ext.gone
import com.arcsoft.commonbase.ext.visible
import com.arcsoft.commonbase.widgets.SwitchButton
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.wuyan.locationdog.R
import com.wuyan.locationdog.view.InputDialog


class AlarmListAdapter(@LayoutRes layoutResId: Int, data: MutableList<DeviceAlarmSetBeanContent>?) :
    BaseQuickAdapter<DeviceAlarmSetBeanContent, BaseViewHolder>(layoutResId, data) {


    override fun convert(holder: BaseViewHolder, item: DeviceAlarmSetBeanContent) {
        val alarmChannel = item.alarmChannel
        holder.apply {
            getView<TextView>(R.id.title).text = item.alarmTypeDesc
            val icon = getView<ImageView>(R.id.icon)
            icon.load(item.icon)

            if (item.alarmTypeDesc.contains("超速报警")) {
                getView<TextView>(R.id.title).text = "超速报警 (" + item.paramValue + "km/h)"

                getView<TextView>(R.id.btnEdit).visible()
                getView<TextView>(R.id.btnEdit).setOnClickListener {
                    val dialog = InputDialog(context, title = "设置报警阈值", hintText = "请输入") {
                        item.paramValue = it
                        getView<TextView>(R.id.title).text = "超速报警 (" + it + "km/h)"
                    }
                    dialog.show()
                    dialog.setNum(item.paramValue?.toInt()?:80)

                }
            } else {
                getView<ImageView>(R.id.btnEdit).gone()
            }
            getView<SwitchButton>(R.id.ifAlarm).isChecked = item.ifAlarm
            getView<SwitchButton>(R.id.ifAlarm).setOnCheckedChangeListener { button, checked ->
                item.ifAlarm = checked
                alarmChannelSize(alarmChannel, item)
            }
            alarmChannelSize(alarmChannel, item)
        }
    }

    private fun BaseViewHolder.alarmChannelSize(
        alarmChannel: List<AlarmChannel>,
        item: DeviceAlarmSetBeanContent
    ) {
        if (alarmChannel.size >= 1) {
            getView<View>(R.id.alarmChannel1).visible()
            getView<TextView>(R.id.name1).text = alarmChannel[0].name
            channelClick(item, alarmChannel, 0, getView<ImageView>(R.id.status1),getView<ImageView>(R.id.alarmChannel1))
        }
//
    }

    private fun BaseViewHolder.channelClick(
        item: DeviceAlarmSetBeanContent,
        alarmChannel: List<AlarmChannel>,
        position: Int,
        imageView: ImageView,
        btnView: View
    ) {
        if (item.ifAlarm) {
            if (alarmChannel[position].status) {
                imageView.setImageResource(R.mipmap.alarm_checked)
            } else {
                imageView.setImageResource(R.mipmap.alarm_unchecked)
            }
        } else {
            if (alarmChannel[position].status) {
                imageView.setImageResource(R.mipmap.alarm_checked_gray)
            } else {
                imageView.setImageResource(R.mipmap.alarm_unchecked)
            }
        }
        btnView.setOnClickListener {
            if (!item.ifAlarm) {
                return@setOnClickListener
            }
            alarmChannel[position].status = !alarmChannel[position].status
            if (alarmChannel[position].status) {
                imageView.setImageResource(R.mipmap.alarm_checked)
            } else {
                imageView.setImageResource(R.mipmap.alarm_unchecked)
            }
        }
    }
}


